package com.ruitu.mall_98_2.order;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.ClipboardUtils;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.Gson;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.mvp.contract.CheckLogisticsContract;
import com.ruitu.mall_98_2.mvp.model.CheckLogisticsModel;
import com.ruitu.mall_98_2.mvp.presenter.CheckLogisticsPresenter;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.DataAdapter;
import com.ruitu.router_module.bean.order.Logistics;
import com.ruitu.router_module.bean.order.OrderDetail;
import com.ruitu.router_module.bean.order.SmallOrder;
import com.ruitu.router_module.bean.product.ProHome;
import com.ruitu.router_module.bean.product.Product;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CheckLogisticsActivity extends MyBaseActivity<CheckLogisticsPresenter, CheckLogisticsModel> implements CheckLogisticsContract.View {
    private LRecyclerView recyclerView_logistics;
    private RecyclerView recyclerView;
    private List<Integer> skipId = new ArrayList<>();
    private DataAdapter adapter;
    private LogisticAdapter logisticAdapter;
    private List<ProHome> products;
    private View header;
    private ImageView iv_commodity;//商品图
    private TextView tv_amount;//数量
    private TextView tv_expressage_state;//物流状态
    private TextView tv_logistic_code;//订单号
    private TextView tv_copy;
    SmallOrder orderDetail;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHeadTitle("物流信息");
        skipId.add(R.id.header);
        showEmptyView(skipId);
        orderDetail = (SmallOrder) getIntent().getExtras().get("smallOrder");
        recyclerView_logistics = findViewById(R.id.recyclerView_logistics);
        header = View.inflate(this, R.layout.item_logistics_header, null);
        recyclerView_logistics = findViewById(R.id.recyclerView_logistics);
        recyclerView = findViewById(R.id.recyclerView);
        iv_commodity = header.findViewById(R.id.iv_commodity);
        tv_amount = header.findViewById(R.id.tv_amount);
        tv_copy = header.findViewById(R.id.tv_copy);
        tv_copy.setOnClickListener(this);
        tv_expressage_state = header.findViewById(R.id.tv_expressage_state);
        tv_logistic_code = header.findViewById(R.id.tv_logistic_code);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView_logistics.setLayoutManager(linearLayoutManager);
        recyclerView_logistics.setPullRefreshEnabled(false);
        logisticAdapter = new LogisticAdapter(this);
        LRecyclerViewAdapter lRecyclerViewAdapter = new LRecyclerViewAdapter(logisticAdapter);
        lRecyclerViewAdapter.addHeaderView(header);
        recyclerView_logistics.setAdapter(lRecyclerViewAdapter);

        GridLayoutManager manager = new GridLayoutManager(this, 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(manager);
        adapter = new DataAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.setDataList(AppHolder.instance().getGlobalProList());

        initData();
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Product product = new Product();
                product.setGoodId(AppHolder.instance().getGlobalProList().get(position).getGoodsId());
                Bundle bundle = new Bundle();
                bundle.putSerializable("product", product);
                try {
                    A_Rutils.startActivity(CheckLogisticsActivity.this, "com.ruitu.mall_98_1.product.activity.ProductDetailActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_copy) {
            ClipboardUtils.copyText(tv_logistic_code.getText());
            ToastUtils.showShort("复制成功");
        }
    }

    private void initData() {
        showProgress();
        mPresenter.getOrderTracesByJson(orderDetail.getId());
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_check_logistics;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return findViewById(R.id.progress);
    }

    @Override
    protected void initViews() {
        super.initViews();

    }

    @Override
    public void setSuc(String logistics) {
        if (TextUtils.isEmpty(logistics)) {
            showContent(skipId);
        } else {
            showContent(null);
            setAdapter(logistics);
        }
    }

    private void setAdapter(String logistics) {
        Gson gson = new Gson();
        Logistics logistics1 = gson.fromJson(logistics, Logistics.class);
        Glide.with(this).load(Config.BASE_URL + orderDetail.getGoodsUrl()).apply(Arad.opts).into(iv_commodity);
        tv_amount.setText(orderDetail.getQuantity() + "件");
        tv_copy.setText("复制");
        if (TextUtils.equals(logistics1.getState(), "0")) {
            tv_expressage_state.setText("物流状态  " + "暂无轨迹信息");
        } else if (TextUtils.equals(logistics1.getState(), "1")) {
            tv_expressage_state.setText("物流状态  " + "已揽件");
        } else if (TextUtils.equals(logistics1.getState(), "2")) {
            tv_expressage_state.setText("物流状态  " + "运输中");
        } else if (TextUtils.equals(logistics1.getState(), "3")) {
            tv_expressage_state.setText("物流状态  " + "已签收");
        } else if (TextUtils.equals(logistics1.getState(), "4")) {
            tv_expressage_state.setText("物流状态  " + "问题件");
        }
        tv_logistic_code.setText("运单号: " + logistics1.getLogisticCode());
        Collections.reverse(logistics1.getTraces());
        logisticAdapter.setDataList(logistics1.getTraces());
//        setProductList();
    }

    @Override
    public void setFaile() {
        showError(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
            }
        });
    }

    class LogisticAdapter extends ListBaseAdapter<Logistics.Traces> {
        public LogisticAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_logistics;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_accept_station = holder.getView(R.id.tv_accept_station);
            TextView tv_accept_time = holder.getView(R.id.tv_accept_time);
            tv_accept_station.setText(mDataList.get(position).getAcceptStation());
            tv_accept_time.setText(mDataList.get(position).getAcceptTime());
            if (position == 0) {
                tv_accept_station.setTextColor(Color.parseColor("#1296db"));
                tv_accept_time.setTextColor(Color.parseColor("#1296db"));
                holder.getView(R.id.line).setVisibility(View.GONE);
            } else {
                tv_accept_station.setTextColor(Color.parseColor("#000000"));
                tv_accept_time.setTextColor(Color.parseColor("#000000"));
                holder.getView(R.id.line).setVisibility(View.VISIBLE);
            }
        }
    }

}
